/*
 * BibliotecaShowDetails.java
 *
 * Created on 1 ottobre 2008, 16.24
 */
package it.biblioteca;

import it.biblioteca.util.Book;

import it.biblioteca.util.BookList;
import it.biblioteca.util.ComboFields;
import it.biblioteca.util.UtilFileOperation;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ImageIcon;

/**
 *
 * @author  giovine
 */
public class BibliotecaShowDetails extends javax.swing.JFrame {

    Book book_passed;
    ArrayList<Book> list_passed;
    ArrayList<Book> found_books;
    BookList my_list;
    ComboFields my_fields;

    /** Creates new form BibliotecaShowDetails */
    public BibliotecaShowDetails(BookList list, int index, ArrayList<Book> list_book, ComboFields fields) {

        this.my_list = list;
        this.found_books = list_book;
        this.list_passed = my_list.getEntireList();
        this.my_fields = fields;
        this.book_passed = found_books.get(index);


        initComponents();
        this.setTitle("BiblioTeCa - Dettagli di " + book_passed.getTitle());

        this.title_tf.setText(book_passed.getTitle());
        this.author_tf.setText(book_passed.getAuthor());
        this.home_tf.setText(book_passed.getHouse());
        this.trans_tf.setText(book_passed.getTranslater());
        this.location_combo.setSelectedItem(book_passed.getLocation());

        if (book_passed.getCopy()) {
            this.copy_check.setSelected(true);
        }

        this.note_area.setText(book_passed.getNote());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        save_bt = new javax.swing.JButton();
        close_bt = new javax.swing.JButton();
        delete_bt = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        copy_check = new javax.swing.JCheckBox();
        location_combo = new javax.swing.JComboBox();
        title_tf = new javax.swing.JTextField();
        home_tf = new javax.swing.JTextField();
        author_tf = new javax.swing.JTextField();
        trans_tf = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        note_area = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("BiblioTeCa - Note Libro");
        setIconImage(new ImageIcon("files/icon_desktop.png").getImage());
        setResizable(false);

        save_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_save.png"))); // NOI18N
        save_bt.setText("Salva Modifiche");
        save_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save_btActionPerformed(evt);
            }
        });

        close_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_close.png"))); // NOI18N
        close_bt.setText("Chiudi");
        close_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_btActionPerformed(evt);
            }
        });

        delete_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_clean.png"))); // NOI18N
        delete_bt.setText("Elimina libro");
        delete_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_btActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel4.setText("Autore del libro:");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel3.setText("Titolo del libro:");

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel6.setText("Locazione del libro:");

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel5.setText("Casa editrice del libro:");

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel7.setText("Traduttore del libro:");

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel8.setText("Note:");

        copy_check.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        copy_check.setText("Fotocopiato");

        ArrayList <String> a = this.my_fields.getFields();

        Iterator i = a.iterator();

        while(i.hasNext()){
            location_combo.addItem((String)i.next());
        }

        note_area.setColumns(20);
        note_area.setLineWrap(true);
        note_area.setRows(5);
        jScrollPane1.setViewportView(note_area);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(copy_check)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(location_combo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(title_tf)
                                .addComponent(home_tf)
                                .addComponent(author_tf, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                .addComponent(trans_tf))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(save_bt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(delete_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(close_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(14, 14, 14)
                        .addComponent(jLabel4)
                        .addGap(14, 14, 14)
                        .addComponent(jLabel5)
                        .addGap(14, 14, 14)
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(title_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(author_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(home_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(location_combo, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(trans_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addComponent(copy_check))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(close_bt)
                    .addComponent(delete_bt)
                    .addComponent(save_bt))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void close_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_btActionPerformed
    UtilFileOperation.saveDataBase(my_list);
    dispose();
}//GEN-LAST:event_close_btActionPerformed

private void delete_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_btActionPerformed
    int i = 0;
    for (Book b : this.list_passed) {
        if ((b.getTitle().equals(book_passed.getTitle())) &&
                (b.getAuthor().equals(book_passed.getAuthor())) &&
                (b.getHouse().equals(book_passed.getHouse())) &&
                (b.getLocation().equals(book_passed.getLocation())) &&
                (b.getTranslater().equals(book_passed.getTranslater())) &&
                (b.getCopy() == book_passed.getCopy())) {
            //Here the remove code.
            this.list_passed.remove(i);
            this.my_list.setBookList(list_passed);
            return;
        }
        i++;
    }

    //Clean all fields
    this.title_tf.setText("");
    this.author_tf.setText("");
    this.home_tf.setText("");
    this.trans_tf.setText("");
    this.note_area.setText("");
    this.copy_check.setSelected(false);

}//GEN-LAST:event_delete_btActionPerformed

private void save_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save_btActionPerformed
    String title = this.title_tf.getText();
    Book book = new Book();
    if (title.equals("")) {
        BibliotecaAddWarning war = new BibliotecaAddWarning("Il campo \"Titolo\" deve essere riempito.");
        war.setLocationRelativeTo(null);
        war.setVisible(true);
    }  else {
            String author = this.author_tf.getText();
            if (author.equals("")) {
                BibliotecaAddWarning war = new BibliotecaAddWarning("Il campo \"Autore\" deve essere riempito.");
                war.setLocationRelativeTo(null);
                war.setVisible(true);
            }  else {
                    String home = this.home_tf.getText();
                    if (home.equals("")) {
                        BibliotecaAddWarning war = new BibliotecaAddWarning("Il campo \"Casa editrice\" deve essere riempito.");
                        war.setLocationRelativeTo(null);
                        war.setVisible(true);
                    } else {
                            String translater = this.trans_tf.getText();
                            
                            String location = (String) this.location_combo.getSelectedItem();

                            book.setTitle(title);
                            book.setAuthor(author);
                            book.setHouse(home);
                            book.setLocation(location);
                            book.setTranslater(translater);
                            book.setNote(this.note_area.getText());

                            if (this.copy_check.isSelected()) {
                                System.out.println("Questo libro è fotocopiato.");
                                book.setCopy(true);
                            } else {
                                System.out.println("Questo libro non è fotocopiato.");
                                book.setCopy(false);
                            }

                            //Instruction to change the book.
                            int i = 0;
                            for (Book b : this.list_passed) {
                                if ((b.getTitle().equals(book_passed.getTitle())) &&
                                        (b.getAuthor().equals(book_passed.getAuthor())) &&
                                        (b.getHouse().equals(book_passed.getHouse())) &&
                                        (b.getLocation().equals(book_passed.getLocation())) &&
                                        (b.getTranslater().equals(book_passed.getTranslater())) &&
                                        (b.getCopy() == book_passed.getCopy())) {
                                    //Here the change code.
                                    this.list_passed.remove(i);
                                    this.list_passed.add(i,book);
                                    this.my_list.setBookList(list_passed);
                                    UtilFileOperation.saveDataBase(my_list);
                                    return;
                                }
                                i++;
                            }


                        }
                    }
                }
            
        
    
}//GEN-LAST:event_save_btActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField author_tf;
    private javax.swing.JButton close_bt;
    private javax.swing.JCheckBox copy_check;
    private javax.swing.JButton delete_bt;
    private javax.swing.JTextField home_tf;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox location_combo;
    private javax.swing.JTextArea note_area;
    private javax.swing.JButton save_bt;
    private javax.swing.JTextField title_tf;
    private javax.swing.JTextField trans_tf;
    // End of variables declaration//GEN-END:variables
}
